<?php
// 数据库配置
$host = 'localhost';
$dbname = 'itops_help1_center';
$username = 'itops_help1_center';
$password = 'itops_help1_center';

$categoryId = 0;
$categoryName = '';
$articles = [];
$error = '';
$categories = [];

try {
    // 获取分类ID（兼容?category=1参数）
    if (isset($_GET['category']) && is_numeric($_GET['category'])) {
        $categoryId = (int)$_GET['category'];
    } else {
        throw new Exception("请通过正确路径访问分类页面");
    }

    // 连接数据库
    $pdo = new PDO(
        "mysql:host=$host;dbname=$dbname;charset=utf8mb4",
        $username,
        $password,
        [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]
    );

    // 获取所有分类（用于导航）
    $stmt = $pdo->query("SELECT id, name FROM categories ORDER BY sort_order ASC");
    $categories = $stmt->fetchAll(PDO::FETCH_ASSOC);

    // 查询当前分类信息
    $stmt = $pdo->prepare("SELECT name FROM categories WHERE id = :id");
    $stmt->execute([':id' => $categoryId]);
    $category = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$category) {
        throw new Exception("该分类不存在或已被删除");
    }
    $categoryName = $category['name'];

    // 查询分类下的文章
    $stmt = $pdo->prepare("SELECT a.*, c.name as category_name 
                        FROM article a 
                        LEFT JOIN categories c ON a.category_id = c.id 
                        WHERE a.category_id = :category_id AND a.status = 1 
                        ORDER BY a.create_time DESC");
    $stmt->execute([':category_id' => $categoryId]);
    $articles = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (Exception $e) {
    $error = $e->getMessage();
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $categoryName ? $categoryName . ' - 知识库' : '分类页面'; ?></title>
    <!-- 统一引用与其他页面一致的CSS资源（核心：引用article.css） -->
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="/frontend/css/article.css"> <!-- 关键：使用目标CSS文件 -->
</head>
<body>
    <!-- 页面结构与类名完全匹配使用article.css的其他页面（如article_detail.php） -->
    <div class="article-page">
        <!-- 页头：类名与article.css定义一致 -->
        <header class="article-header">
            <div class="header-wrap">
                <h1 class="site-title">
                    <a href="/index.php" class="title-link">IT知识库</a>
                </h1>
                <p class="site-desc">整理各类操作指南和说明文档</p>
            </div>
        </header>

        <!-- 导航栏：类名匹配article.css中的导航样式 -->
        <nav class="article-nav">
            <div class="nav-wrap">
                <ul class="nav-list">
                    <li class="nav-item"><a href="/index.php" class="nav-link">首页</a></li>
                    <?php foreach ($categories as $cat): ?>
                        <li class="nav-item">
                            <a href="category.php?category=<?php echo $cat['id']; ?>" 
                               class="nav-link <?php echo $cat['id'] == $categoryId ? 'active' : ''; ?>">
                                <?php echo htmlspecialchars($cat['name']); ?>
                            </a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </nav>

        <!-- 主内容区：类名与article.css中的内容容器一致 -->
        <main class="article-content">
            <div class="content-wrap">
                <!-- 面包屑导航：复用article.css中的面包屑样式 -->
                <div class="bread-nav">
                    <a href="/index.php" class="bread-item">首页</a>
                    <span class="bread-sep">/</span>
                    <span class="bread-item current"><?php echo htmlspecialchars($categoryName); ?></span>
                </div>

                <!-- 分类标题区：类名匹配article.css中的标题样式 -->
                <div class="page-title-box">
                    <h2 class="page-title"><?php echo htmlspecialchars($categoryName); ?> 分类文章</h2>
                    <a href="/index.php" class="back-link">
                        <i class="fas fa-arrow-left"></i> 返回首页
                    </a>
                </div>

                <!-- 错误提示：使用article.css中的提示样式类 -->
                <?php if ($error): ?>
                    <div class="msg-error">
                        <i class="fas fa-exclamation-circle"></i> <?php echo $error; ?>
                    </div>
                <?php endif; ?>

                <!-- 文章列表：类名与article.css中的文章卡片样式完全匹配 -->
                <div class="article-list">
                    <?php if (empty($articles) && !$error): ?>
                        <!-- 空状态：复用article.css中的空状态样式 -->
                        <div class="empty-box">
                            <i class="fas fa-file-alt"></i>
                            <p>该分类下暂无文章</p>
                        </div>
                    <?php endif; ?>

                    <?php foreach ($articles as $article): ?>
                        <div class="article-card">
                            <h3 class="card-title">
                                <a href="article_detail.php?id=<?php echo $article['id']; ?>" class="card-link">
                                    <?php echo htmlspecialchars($article['title']); ?>
                                </a>
                            </h3>
                            <div class="card-meta">
                                <span class="meta-item">
                                    <i class="fas fa-folder"></i> <?php echo htmlspecialchars($article['category_name'] ?? '未分类'); ?>
                                </span>
                                <span class="meta-item">
                                    <i class="fas fa-calendar"></i> <?php echo $article['create_time']; ?>
                                </span>
                            </div>
                            <div class="card-excerpt">
                                <?php 
                                $content = strip_tags(preg_replace('/\#+\s+/', '', $article['content']));
                                echo mb_substr($content, 0, 150, 'utf-8') . '...'; 
                                ?>
                            </div>
                            <a href="article_detail.php?id=<?php echo $article['id']; ?>" class="read-more">
                                查看详情 <i class="fas fa-angle-right"></i>
                            </a>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </main>

        <!-- 页脚：类名与article.css中的页脚样式一致 -->
        <footer class="article-footer">
            <div class="footer-wrap">
                <p class="copyright">© 2025 IT知识库 版权所有</p>
            </div>
        </footer>
    </div>
</body>
</html>